<template>
  <el-form ref="form" :model="form" :rules="formRules" style="margin-top: 20px" label-position="top">
    <el-row>
      <el-col :span="3">
        <el-upload
            class="avatar-uploader"
            action=""
            :auto-upload="false"
            :show-file-list="false"
            :on-change="handleAvatarChange"
            :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-col>
      <el-col :span="21">
        <el-form-item label="Group Name" prop="name" required>
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="Properties" prop="properties" required>
          <el-select v-model="form.properties" placeholder="Please Select" style="width: 100%" popper-class="properties" @change="isDisableCheckbox">
            <el-option v-for="item in propertiesOptions" :label="item.label" :value="item.value" :key="item.value">
              <div>
                <span style="font-weight: bold">{{item.label}}</span>
                <div class="properties-desc">{{item.desc}}</div>
              </div>
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="Affiliation" prop="affiliation" required>
          <el-select v-model="form.affiliation" placeholder="Please Select" style="width: 100%">
            <el-option v-for="item in affiliationOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-form-item>
      <el-checkbox v-model="form.publicMembers" :disabled="form.properties === 'Private'">Public group members</el-checkbox>
    </el-form-item>
    <el-form-item label="Description" prop="description" required>
      <el-input type="textarea" resize="none" :autosize="{ minRows: 3, maxRows: 6}" v-model="form.description"></el-input>
    </el-form-item>
    <el-row :gutter="20">
      <el-col :span="18">
        <el-form-item label="Group Meets" prop="meets.meetsDate">
          <el-date-picker type="datetime" placeholder="Please Select" v-model="form.meets.meetsDate" style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="*" class="hide-item">
           {{getTz()}}
        </el-form-item>
      </el-col>
    </el-row>
    <el-form-item prop="meets.repeat" required>
      <el-select v-model="form.meets.repeat" placeholder="Please Select">
        <el-option v-for="item in groupMeetsOptions" :key="item" :label="item" :value="item"></el-option>
      </el-select>
    </el-form-item>
  </el-form>
</template>

<script>
import moment from 'moment'
export default {
  name: 'newGroupStep1',
  props: ['form'],
  data () {
    const validateName = (rule, value, callback) => {
      value = value.trim()
      if (value === '') {
        return callback(new Error('Please Enter Name'))
      }
      if (value.length < 1 || value.length > 50) {
        return callback(new Error('1 to 50 characters in length'))
      }
      callback()
    }
    return {
      imageUrl: '',
      formRules: {
        name: [
          { validator: validateName, trigger: 'blur' }
        ],
        'meets.meetsDate': [
          { required: true, message: 'Please enter', trigger: 'blur' }
        ]
      },
      propertiesOptions: [
        { label: 'Private', value: 'Private', desc: 'Only those invited by the captain can join the group.' },
        { label: 'Public', value: 'Public', desc: 'Anyone can join the group by applying to the group captain.' }
      ],
      groupMeetsOptions: ['No Repeats', 'Daily', 'Weekly', 'Monthly', 'Quarterly', 'Annually']
    }
  },
  watch: {
    form (val) {
      this.$emit('update:form', val)
    }
  },
  computed: {
    affiliationOptions () {
      const data = window.SITE_CONFIG['dictList'].find(e => e.dictType === 'get_started_about_peer_groups').dataList
      const arr = data.map(item => {
        return { label: item.dictLabel, value: item.dictLabel }
      })
      return arr.concat([
        { label: 'Encore', value: 'Encore' },
        { label: 'MSP Advisor', value: 'MSP Advisor' },
        { label: 'Bering McKinley', value: 'Bering McKinley' }
      ])
    }
  },
  methods: {
    async submitForm () {
      return this.$refs['form'].validate()
    },
    handleAvatarChange (file, fileList) {
      const that = this
      const reader = new FileReader()
      reader.readAsDataURL(file.raw)
      reader.onload = function () {
        that.imageUrl = this.result
      }
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('The uploaded avatar can only be in JPG format!')
      }
      if (!isLt2M) {
        this.$message.error('The size of the uploaded avatar image cannot exceed 2MB!')
      }
      return isJPG && isLt2M
    },
    isDisableCheckbox (val) {
      if (val === 'Private') {
        this.form.publicMembers = false
      }
    },
    getTz () {
      return moment.tz.guess()
    }
  }
}
</script>

<style>
.properties .el-select-dropdown__item{
  height: auto;
}
</style>
<style scoped lang="scss">
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
::v-deep .el-form-item__label{
  font-weight: bold;
}
.properties-desc{
  font-size: 12px;
  color: #0000009B;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 78px;
  height: 78px;
  line-height: 78px;
  text-align: center;
}
.avatar {
  width: 78px;
  height: 78px;
  display: block;
}
::v-deep .el-upload::after {
  content: 'Upload';
  display: block;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  line-height: 22px;
  background: #00000072;
  color: #FFFFFF;
  font-size: 12px;
}
::v-deep .hide-item .el-form-item__label {
  visibility: hidden;
}
</style>
